/**
 * Created by chaofang on 2018/8/17.
 */
import React, {Component, PropTypes} from 'react';
import {
    Image,
    StyleSheet,
    Text,
    View,
    NativeModules,
    TouchableOpacity,
    Modal,
    Dimensions, Platform
} from 'react-native';

import moment from "moment";
import Toast from "react-native-simple-toast";
import AppService from "../../service/AppService";

export default class FounderList extends Component {
    // 初始化
    constructor(props) {
        super(props);
        this.state = {
            userId: '',
            userName: '',
            founderList: [],
            founderIncomeList: [],
            modalShow: false,
        };
    };

    componentWillMount() {
        const _this = this;
        AppService.searchProjectIncome({pageSize: 0, tenantId: global.tenantId, dbType: 1}).then((data) => {
            this.setState({
                founderIncomeList: data.result,
            });
        });

        AppService.getSuggestConfig({id: this.props.suggestId, dealType: 0}).then((data) => {
            if (!!data.errors === true && !!data.errors.length > 0) {
                Toast.show(data.errors[0].message);
            } else {
                _this.setState({
                    founderList: data.suggestConfigList,
                });

            }
        });
    }

    enlargeImg(url) {
        this.setState({
            modalShow: true,
            filePath: url
        });
    }

    closeModal() {
        this.setState({
            modalShow: false
        });
    }

    renderCommentImage = (v, i) => {
        return (
            <TouchableOpacity onPress={() => this.enlargeImg(v.filePath)} key={i}>
                <Image style={styles.commentImage} source={{uri: v.filePath}}/>
            </TouchableOpacity>
        )
    };

    sortNumber(a, b) {
        return b.postTime - a.postTime;
    }

    renderFounderIncome = (v, i) => {
        if (v === null || v.length === 0)
            return;
        var items = v.split(":");
        var incomeItem;
        for (var i = 0; i < this.state.founderIncomeList.length; i++) {
            if (items[0] === this.state.founderIncomeList[i].id) {
                incomeItem = this.state.founderIncomeList[i];
            }
        }
        return (
            <View>
                <Text style={{
                    color: '#000',
                    fontSize: 10
                }}>{incomeItem.projectIncomeName}{items[1]}{incomeItem.projectIncomeUnit}</Text>
            </View>
        )
    }

    renderFounder = (v, i) => {
        if (v.projectProfitConfig == null || v.projectProfitConfig.length == 0) {
            return (
                <View style={styles.commentDetail} key={i}>
                    <View style={{flex: 2, flexDirection: 'row'}}>
                        <Text style={{color: '#000', fontSize: 12}}>{v.dealName}</Text>
                        {v.deal ? <Text style={styles.founderStatus}>已处理</Text> :
                            <Text style={styles.founderUnDoStatus}>未处理</Text>}
                    </View>
                    <View>
                        <Text style={{color: '#000', fontSize: 10}}>{v.projectSummary}</Text>
                    </View>
                    <View>
                        <Text style={{color: '#000', fontSize: 10}}>{v.projectProfit}</Text>
                    </View>


                </View>
            )
        } else {
            return (
                <View style={styles.commentDetail} key={i}>
                    <View style={{flex: 2, flexDirection: 'row'}}>
                        <Text style={{color: '#000', fontSize: 12}}>{v.dealName}</Text>
                        {v.deal ? <Text style={styles.founderStatus}>已处理</Text> :
                            <Text style={styles.founderUnDoStatus}>未处理</Text>}
                    </View>
                    <View>
                        <Text style={{color: '#000', fontSize: 10}}>{v.projectSummary}</Text>
                    </View>
                    <View>
                        <Text style={{color: '#000', fontSize: 10}}>{v.projectProfit}</Text>
                    </View>
                    <View>
                        {v.projectProfitConfig.split(";").map((value, idx) => this.renderFounderIncome(value, idx))}
                    </View>

                </View>
            )
        }
    };

    render() {
        return (
            <View style={styles.commentWrap}>
                <Modal
                    animationType='slide'
                    transparent={true}
                    visible={this.state.modalShow}
                    onShow={() => {
                    }}
                    onRequestClose={() => {
                    }}>
                    <TouchableOpacity activeOpacity={1} style={styles.modalLayout} onPress={() => {
                        this.closeModal()
                    }}>
                        <Image style={styles.modalView}
                               source={{uri: this.state.filePath + "?x-oss-process=image/resize,m_lfit,w_640,h_640"}}
                               resizeMode="contain"></Image>
                    </TouchableOpacity>
                </Modal>
                {this.state.founderList.map((v, i) => this.renderFounder(v, i))}
                {this.state.founderList.length == 0 ? <Text style={{color: '#000', fontSize: 12}}>无</Text> :
                    <Text style={{color: '#000', fontSize: 12}}></Text>}
            </View>
        );
    };
}

const styles = StyleSheet.create({
    modalLayout: {
        width: Dimensions.get('window').width,
        height: Dimensions.get('window').height,
        backgroundColor: 'rgba(0,0,0,.8)'
    },
    modalView: {
        width: Dimensions.get('window').width,
        height: Dimensions.get('window').height
    },
    commentWrap: {
        flex: 1,
        paddingLeft: 20,
        paddingRight: 20,
        paddingBottom: 20,
        paddingTop: 5
    },
    commentDetail: {
        // flexDirection:'row',
        marginBottom: 10
    },
    commentAvatar: {
        width: 40,
        height: 40,
        marginRight: 15
    },
    commentImageWrap: {
        flex: 1,
        flexDirection: 'row',
        flexWrap: 'wrap',
        marginTop: 10
    },
    founderImage: {
        width: 50,
        height: 50,
        margin: 10
    },
    founderStatus: {
        color: '#32CD32',
        fontSize: 12,
        width: 40,
        position: 'absolute',
        right: 0,
        top: 0,
        height: Platform.OS === 'android' ? 50 : 60,
        justifyContent: 'center',
        paddingTop: Platform.OS === 'ios' ? 20 : 0,
    },
    founderUnDoStatus: {
        color: '#FF2D2D',
        fontSize: 12,
        width: 40,
        position: 'absolute',
        right: 0,
        top: 0,
        height: Platform.OS === 'android' ? 50 : 60,
        justifyContent: 'center',
        paddingTop: Platform.OS === 'ios' ? 20 : 0,
    },
});
